home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 21
/
Cream of the Crop 21 (Terry Blount) (October 1996).iso
/
bbs
/
pad311.zip
/
COLOUR.MH
< prev
next >
Wrap
Text File
|
1996-05-13
|
7KB
|
202 lines
#ifndef __COLOUR_MH
#define __COLOUR_MH
// Function list:
//
// int nameToColour (string: colourName);
// string getColourString (int: fg, int: bg);
// string stringToColourString (string: inputString);
// void changeColour (int: fg, int: bg);
// string outString (string: inputString);
// void colourInit ();
// bool colourToken (string: token, string: params);
//
// Colour constants, to be used with the colour functions below
#define C_BLACK 0
#define C_BLUE 1
#define C_GREEN 2
#define C_CYAN 3
#define C_RED 4
#define C_MAGENTA 5
#define C_BROWN 6
#define C_GRAY 7
#define C_DKGRAY 8
#define C_LBLUE 9
#define C_LGREEN 10
#define C_LCYAN 11
#define C_LRED 12
#define C_LMAGENTA 13
#define C_YELLOW 14
#define C_WHITE 15
// int nameToColour (string: colourName)
//
// Converts a string containing the name of a colour into an int equal
// to one of the C_* colour constants, below. These colour constants
// may be used with the getColourString and changeColour functions.
int nameToColour (string: colourName) {
colourName := strupper (colourName);
if (colourName = "BLACK") return 0;
if (colourName = "BLUE") return 1;
if (colourName = "GREEN") return 2;
if (colourName = "CYAN") return 3;
if (colourName = "RED") return 4;
if (colourName = "MAGENTA") return 5;
if (colourName = "BROWN") return 6;
if (colourName = "GRAY") return 7;
if ((colourName = "DKGRAY")
or (colourName = "DARKGRAY")) return 8;
if ((colourName = "LBLUE")
or (colourName = "LIGHTBLUE")) return 9;
if ((colourName = "LGREEN")
or (colourName = "LIGHTGREEN")) return 10;
if ((colourName = "LCYAN")
or (colourName = "LIGHTCYAN")) return 11;
if ((colourName = "LRED")
or (colourName = "LIGHTRED")) return 12;
if ((colourName = "LMAGENTA")
or (colourName = "LIGHTMAGENTA")) return 13;
if (colourName = "YELLOW") return 14;
if (colourName = "WHITE") return 15;
return -1;
}
// string getColourString (int: fgCol, int: bgCol)
//
// Given a foreground and background colour (equal to one of the
// C_* colour constants), this function returns a string which,
// when printed, will change the text colour to that colour.
string getColourString (int: fgCol, int: bgCol) {
string: result;
if (fgCol = -1) return "";
if (bgCol = -1) bgCol := 0;
result := "\x16\x01";
result [3] := fgCol + 16*bgCol;
return result;
}
// string stringToColourString (string: s);
//
// Converts a mecca-like colour command into an AVATAR colour code. When this
// colour code is printed, it will change the screen colour.
//
// Examples of mecca-like colour commands handled by this function:
//
// blue on black
// yellow
// yellow ON blue
//
// If the string "on" ommitted, the background colour is assumed to be black.
//
string stringToColourString (string: s) {
int: pos, fg;
string: sub;
pos := 0;
sub := strtok (s," \t", pos);
if (sub <> "") fg := nameToColour (sub);
sub := strupper (strtok (s," \t", pos));
if (sub <> "ON") return getColourString (fg, 0);
sub := strtok (s, " \t", pos);
return getColourString (fg, nameToColour (sub));
}
// void changeColour (int: fgCol, int: bgCol)
//
// Changes the text colour, given a C_* constant for the new foreground
// and background colours.
void changeColour (int: fgCol, int: bgCol) {
print (getColourString (fgCol, bgCol));
}
// Converts a string containing mecca colour commands and Mex escape codes
// into a string that, when displayed, will actually show the correct colours
// and special characters.
//
// For example,
//
// [yellow on blue]Hello [green]world\n[white ]This is a test\n
//
// Would be converted into a string which, when displayed, would appear as:
//
// Hello world
// This is a test
//
// The string "Hello" would be yellow on a blue background, the string "world"
// would be green on black, and the string "This is a test" would be white.
//
// This is useful for configuring colours and output strings from input files.
//
string outString (string: src) {
int: sidx, tidx, slen, cmd, tmp1, tmp2;
string: result;
slen := strlen (src);
tidx := 1;
for (sidx := 1; sidx <= slen; sidx := sidx + 1) {
if ((src [sidx] = '\\') and (sidx < slen)) {
sidx := sidx + 1;
cmd := src [sidx];
if (cmd = '\\') {result [tidx] := '\\';}
else if (cmd = 'n') {result [tidx] := '\n';}
else if (cmd = 'r') {result [tidx] := '\r';}
else if (cmd = 'a') {result [tidx] := '\a';}
else if (cmd = 'b') {result [tidx] := '\b';}
else if (cmd = 'f') {result [tidx] := '\f';}
else if (cmd = '\'') {result [tidx] := '\'';}
else if (cmd = '[') {result [tidx] := '[';}
else if (cmd = '\"') {result [tidx] := '\"';}
else if ((cmd = 'x') and (sidx < slen + 2)) {
tmp1 := hexDig (src [sidx+1]);
tmp2 := hexDig (src [sidx+2]);
if ((tmp1 > -1) and (tmp2 > -1)) {
result [tidx] := 16 * tmp1 + tmp2;
};
sidx := sidx + 2;
};
tidx := tidx + 1;
}
else if (src [sidx] = '[') {
sidx := sidx + 1;
if (src [sidx] = '[') {
result [tidx] := '[';
tidx := tidx + 1;
}
else {
tmp1 := stridx (src, sidx, ']');
result := result + stringToColourString (
strtrim (substr (src, sidx, tmp1 - sidx), " \t"));
tidx := strlen (result) + 1;
sidx := tmp1;
};
}
else {
result [tidx] := src [sidx];
tidx := tidx + 1;
};
};
return result;
}
void colourInit () {
// Nothing to do!
}
bool colourToken (string: token, string: params) {
// Nothing to do!
return False;
}
#endif